<template>
  <div class="login">
    <div class="login-form">
      <div class="login-form__title">登陆</div>
      <el-form ref="ruleFormRef" :model="form" label-width="80px" >
        <el-form-item label="手机号码" prop="mobile" required error="请输入手机号码">
          <el-input v-model="form.mobile" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="密码" prop="password" required error="请输入密码">
          <el-input v-model="form.password" type="password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">登录</el-button>
          <el-button>注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ElMessage, FormInstance } from 'element-plus';
import { reactive, ref } from 'vue';
import { useRouter } from 'vue-router';
import { login } from '../api/auth';

const form = reactive({
  mobile: '',
  password: '',
});

const router = useRouter();
const ruleFormRef = ref<FormInstance>();

const onSubmit = async () => {
  if (!ruleFormRef.value) return;
  const valid = await ruleFormRef.value.validate((_) => _);
  if (!valid) return;

  await login(form);
  ElMessage.success('登录成功');
  router.push('/home');
};
</script>

<style lang="scss" scoped>
.login {
  background-color: var(--el-color-primary);
  height: 100%;
  position: relative;

  &-form {
    position: absolute;
    background-color: white;
    border-radius: 20px;
    top: 50%;
    right: 100px;
    transform: translateY(-50%);
    width: 300px;
    padding: 20px;

    &__title {
      font-size: 20px;
      margin-bottom: 10px;
    }
  }
}
</style>
